/*
 * Copyright 2011 group coding kata nantes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package fr.coding.kata.nantes.roman.sylvain;

import fr.coding.kata.nantes.roman.commons.NumeralsToRomanInterface;

public class NumeralsToRoman implements NumeralsToRomanInterface {

    private static final String REPRESENTATION_UNKNOWN = "Representation of {} does not exists at the power of ten: {}";

    private int numeral;

    @Override
    public NumeralsToRomanInterface with(final Integer numerals) {
        numeral = numerals;
        return this;
    }

    @Override
    public String call() {
        return toRoman(numeral, new StringBuilder()).toString();
    }

    private StringBuilder toRoman(final int numeral, final StringBuilder roman) {
        final String numeralString = Integer.toString(numeral);
        final int maxPowOfTen = numeralString.length() - 1;
        for (int i = 0; i <= maxPowOfTen; i++) {
            final char digit = numeralString.charAt(i);
            roman.append(toRoman(digit, maxPowOfTen - i));
        }

        return roman;
    }

    private String toRoman(final char digit, final int powerOfTen) {
        final int numeral = Character.digit(digit, 10);
        final String oneAtPowerOfTen = oneAtPowerOfTen(powerOfTen);

        if (numeral < 4) {
            switch (numeral) {
            case 0:
                return "";
            case 1:
                return oneAtPowerOfTen;
            case 2:
                return oneAtPowerOfTen + oneAtPowerOfTen;
            case 3:
                return oneAtPowerOfTen + oneAtPowerOfTen + oneAtPowerOfTen;
            }
        } else {
            final String fiveAtPowerOfTen = fiveAtPowerOfTen(powerOfTen);
            switch (numeral) {
            case 4:
                return oneAtPowerOfTen + fiveAtPowerOfTen;
            case 5:
                return fiveAtPowerOfTen;
            case 6:
                return fiveAtPowerOfTen + oneAtPowerOfTen;
            case 7:
                return fiveAtPowerOfTen + toRoman('2', powerOfTen);
            case 8:
                return fiveAtPowerOfTen + toRoman('3', powerOfTen);
            case 9:
                return oneAtPowerOfTen + oneAtPowerOfTen(powerOfTen + 1);
            }
        }

        throw new IllegalArgumentException("Invalide number: " + numeral);
    }

    private String oneAtPowerOfTen(final int powerOfTen) {
        switch (powerOfTen) {
        case -1:
            return "";
        case 0:
            return "I";
        case 1:
            return "X";
        case 2:
            return "C";
        case 3:
            return "M";
        default:
            throw new IllegalArgumentException(String.format(REPRESENTATION_UNKNOWN, 1,  powerOfTen));
        }
    }

    private String fiveAtPowerOfTen(final int powerOfTen) {
        switch (powerOfTen) {
        case 0:
            return "V";
        case 1:
            return "L";
        case 2:
            return "D";
        default:
            throw new IllegalArgumentException(String.format(REPRESENTATION_UNKNOWN, 5,  powerOfTen));
        }
    }

}
